package com.seda.fdc.ejb.reports.pdfgenerator.lowagie.text;

import java.awt.Color;

import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph; 
import com.lowagie.text.pdf.*;

public abstract class AbstractTextBox{

	static int PAINTING = 0;
	static int PAINTED = 1;
	
	 
	static final Font defaultFont = new Font(1, 11);
	
	private PdfPTable table;
	private PdfPCell cell;
	private Font font; 
	private float defaultLeading; 
	private int stato;
	
	
	private int horizontalAlignment;

	Paragraph tempParagraph;
	
	public AbstractTextBox(){
		init();
	}
	

	public AbstractTextBox(float x, float y){  
		init();
		table.setTotalWidth(x);
		table.setLockedWidth(true);
		cell.setFixedHeight(y); 
	}
	
	
	private void init(){ 
		stato = PAINTING;
		
		table = new PdfPTable(1);
		table.getDefaultCell().setBorderWidth(0f); 
		cell = new PdfPCell();
		cell.setPadding(0f);
		cell.setBorderWidth(0f);
		font = defaultFont;
		defaultLeading = Float.NaN;
	}
	
	
	public void setDimension(float x, float y) throws IllegalStateException{
		checkStato();
		table.setTotalWidth(x);
		table.setLockedWidth(true);
		cell.setFixedHeight(y); 
	}
	



	public void setHorizontalAlignment(int horizontalAlign) throws IllegalStateException{
		checkStato();
		horizontalAlignment = horizontalAlign;
	}
	
	protected void setAbsolutePosition(float x, float y, PdfContentByte pdfContentByte) throws IllegalStateException{ 
		checkStato();
		closeTempParagraph();
		
		table.addCell(cell);
		table.writeSelectedRows(0, -1, x, y, pdfContentByte); 
		stato = PAINTED;
	}

	public void setAbsolutePosition(float x, float y, PdfWriter writer) throws IllegalStateException{ 
		checkStato();
		closeTempParagraph();
		
		table.addCell(cell);
		table.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
		stato = PAINTED;
	}



	public void setFont(BaseFontFactory bf)   throws IllegalStateException{ 
		checkStato();
		font = new Font(bf); 
		} 
	
	public void setFont(BaseFontFactory bf, float size)   throws IllegalStateException{ 
		checkStato();
		font = new Font(bf,size) ; 
	}
	
	public void setFont(BaseFontFactory bf, float size, int style)   throws IllegalStateException{
		checkStato();
		font = new Font(bf, size, style) ; 
		}
	
	public void setFont(BaseFontFactory bf, float size, int style, Color color)   throws IllegalStateException{
		checkStato();
		font = new Font(bf, size, style, color) ; 
		}
	
	public void setFont(Font other)   throws IllegalStateException{ 
		checkStato();
		font = new Font(other) ;
		}
	
	public void setFont(int family)   throws IllegalStateException{ 
		checkStato();
		font = new Font(family) ; 
		}
	
	public void setFont(int family, float size)   throws IllegalStateException{
		checkStato();
		font = new Font(family, size) ; 
		}
	
	public void setFont(int family, float size, int style)   throws IllegalStateException{
		checkStato();
		font = new Font(family, size, style); 
		}
	
	public void setFont(int family, float size, int style, Color color)  throws IllegalStateException{
		checkStato();
		font = new Font(family, size, style, color) ; 
		}
	 

	public void addText(String text, Font font) throws IllegalStateException{ 
		checkStato();
		
		Chunk chunk = new Chunk(text, font); 
		
		if (tempParagraph == null) {
			tempParagraph =  new Paragraph();
			
			tempParagraph.setSpacingAfter(0);
			tempParagraph.setSpacingBefore(0);
			tempParagraph.setAlignment( horizontalAlignment );
			
			tempParagraph.setLeading( defaultLeading ); 
			
		}

		tempParagraph.add(chunk); 
		}

	public void addText(String text) throws IllegalStateException{ 
		checkStato();
		
		Chunk chunk = new Chunk(text, font); 
		
		if (tempParagraph == null) {
			tempParagraph =  new Paragraph();
			
			tempParagraph.setSpacingAfter(0);
			tempParagraph.setSpacingBefore(0);
			tempParagraph.setAlignment( horizontalAlignment );
			
			if (defaultLeading == Float.NaN) { 
				tempParagraph.setLeading( (float) (1.5 * font.getSize()) );
			} else{
				tempParagraph.setLeading( defaultLeading ); 
			}
		}

		tempParagraph.add(chunk); 
		}
	
	
	public void addLine(String line) throws IllegalStateException{ 
		checkStato();
		closeTempParagraph();
		
		if (line.trim().equals("")) {
			line = " ";
		}
		Paragraph paragraph =  new Paragraph(line, font); 
		paragraph.setSpacingAfter(0);
		paragraph.setSpacingBefore(0);
		
		if (defaultLeading == Float.NaN) { 
			paragraph.setLeading( (float) (1.5 * font.getSize()) );
		} else{
			paragraph.setLeading( defaultLeading ); 
		}
		
		
		paragraph.setAlignment( horizontalAlignment );
		 
		cell.addElement(paragraph);
		}
	

	public void setLeading(float leading) throws IllegalStateException{
		checkStato();
		defaultLeading = leading;
	}
	
	public void setLeading(double leading) throws IllegalStateException{
		checkStato();
		defaultLeading = (float)leading;
	}

	public void setLeading(int leading) throws IllegalStateException{
		checkStato();
		defaultLeading = (float)leading;
	}

	
	public void setRotation(int angle) throws IllegalStateException{
		checkStato();
		cell.setRotation(angle);
	} 
	
	public void setBorderWidth(float width) throws IllegalStateException{
		checkStato();
		cell.setBorderWidth(width);
	}
	
	public void setBorderColor(Color arg0) throws IllegalStateException{
		checkStato();
		cell.setBorderColor(arg0);
	}
	
	

	private void checkStato() throws IllegalStateException{ 
		if (stato == PAINTED) {
			throw new IllegalStateException("Impossibile chiamare il metodo suddetto dopo aver posizionato l'oggetto");
		} 
	}

	private void closeTempParagraph(){ 
		if (tempParagraph != null) {
			cell.addElement(tempParagraph);
			tempParagraph = null;
		}
	}
}
